package com.quanshu.exchange.ui.gui.profile.member;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.ShopInterface;
import com.quanshu.exchange.support.entity.MemberOrderEntity;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.BaseFragment;
import com.quanshu.exchange.ui.base.MyApplication;
import com.quanshu.exchange.ui.base.recycler.BaseViewHolder;
import com.quanshu.exchange.ui.base.recycler.DataDisplayer;
import com.quanshu.exchange.ui.base.recycler.LayoutWrapper;
import com.quanshu.exchange.ui.base.recycler.SingleAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 页面
 */

public class MemberOrderFragment extends BaseFragment {

    @BindView(R.id.member_order_null)
    LinearLayout myOrderNull;
    @BindView(R.id.order_recy)
    RecyclerView orderRecy;
    private List<MemberOrderEntity> datas = new ArrayList<>();//全部数据
    private SingleAdapter<MemberOrderEntity> adapter;
    private int type;

    public static MemberOrderFragment newInstance(int type) {
        MemberOrderFragment f = new MemberOrderFragment();
        Bundle args = new Bundle();
        args.putInt("type", type);
        f.setArguments(args);
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        return rootView;
    }

    @Override
    public int setFragmentLayoutID() {
        return R.layout.fragment_member_order_all;
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        getOrderList();
    }

    @Override
    protected void initView() {

        orderRecy.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new SingleAdapter<>(new DataDisplayer<MemberOrderEntity>(getActivity(), R.layout.item_member_order) {
            @Override
            public void bindData(BaseViewHolder holder,
                                 LayoutWrapper<MemberOrderEntity> wrapper,
                                 MemberOrderEntity item,
                                 int position) {

                holder.setText(R.id.tv_order_no, "订单号： " + item.getOrder_no());
                holder.setText(R.id.tv_date, item.getCreate_time());
                holder.setText(R.id.tv_order_money, "订单金额： " + item.getSell_price());
                holder.setText(R.id.tv_order_bonus, "平台佣金： " + item.getSum_price());

                TextView statusView = holder.getView(R.id.tv_status);
                if (item.getDistribution_status().equals("已收益")) {
                    statusView.setTextColor(getResources().getColor(R.color.text_color_ef5151));
                } else {
                    statusView.setTextColor(getResources().getColor(R.color.text_color_bbd943));
                }
                statusView.setText(item.getDistribution_status());

                holder.setOnClickListener(v -> {
                    Intent intent = new Intent(getActivity(), OrderInfoDetailActivity.class);
                    intent.putExtra("MemberOrderEntity", item);
                    startActivity(intent);
                });

            }
        });
        orderRecy.setAdapter(adapter);
    }

    /**
     * 不会执行setUserVisibleHint方法，但是会执行onResume()方法
     *
     * @param isVisibleToUser
     */

    //左右相邻的Fragment切换时执行
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    /**
     * 左右相邻的Fragment切换，
     * 并不会执行onResume()方法，但是会执行setUserVisibleHint方法
     */
    //从详情页回退到列表页时执行
    @Override
    public void onResume() {
        super.onResume();
    }

    //全部订单
    private void getOrderList() {
        type = getArguments().getInt("type");
        NetClient.getInstance().getShopService(ShopInterface.class)
                .getMemberOrderList(MyApplication.getInstance().getCurrentUser().getMemberid(), type)
                .compose(new BaseTransformer<>((BaseActivity) getActivity()))
                .subscribe(new BaseObserver<BaseResponse<List<MemberOrderEntity>>>() {
                    @Override
                    public void onSuccess(BaseResponse<List<MemberOrderEntity>> response) {
                        if (response.getData() != null && response.getData().size() > 0) {
                            datas = response.getData();
                            if (datas != null && datas.size() > 0) {
                                adapter.setData(datas);
                                if (myOrderNull != null) {
                                    myOrderNull.setVisibility(View.GONE);
                                }
                            } else {
                                if (myOrderNull != null) {
                                    myOrderNull.setVisibility(View.VISIBLE);
                                }
                            }
                        } else {
                            if (myOrderNull != null) {
                                myOrderNull.setVisibility(View.VISIBLE);
                            }
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        super.onFailure(e);
                    }
                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

}
